﻿package com.badibadi.activity;

import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

//新建俱乐板界面（可转化为修改聚乐板）
public class CreateNewPoly1Activity extends BaseActivity implements
		OnClickListener {

	String TAG = "CreateNewPoly1Activity";
	// 以下用于点击设置权限后打钩
	public ImageView i_0, i_1, i_2;
	// 点击layout后ImageView打钩
	public LinearLayout ll_0;
	public LinearLayout ll_1;
	public LinearLayout ll_2;

	private int jurisdiction = 0;// 用于判断选择的什么权限（0所有人/1朋友/2仅自己）

	// 以下用于点击设置权限后打钩
	public ImageView i_10;
	public ImageView i_11;
	public ImageView i_12;
	// 点击layout后ImageView打钩
	public LinearLayout ll_10;
	public LinearLayout ll_11;
	public LinearLayout ll_12;

	private int jurisdiction2 = 10;// 用于判断选择的什么版型（10足迹/11梦想/12收藏）

	private String uid;
	public EditText create_new_poly1_name;
	private Results results;
	public Button create_new_poly1_finish;
	public TextView create_new_poly1_tital;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(CreateNewPoly1Activity.this);
				try {
					Utils.showMessage(CreateNewPoly1Activity.this,
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}

				break;
			case 2:
				Utils.ExitPrgress(CreateNewPoly1Activity.this);
				if (results != null) {
					try {
						if (results.isRet() == true) {
							Utils.showMessage(CreateNewPoly1Activity.this,
									getResources().getString(R.string.l_xb58));
							Intent intent = new Intent(
									CreateNewPoly1Activity.this,
									MyPoly1Activity.class);
							startActivity(intent);
							CreateNewPoly1Activity.this.finish();
						} else {
							Utils.showMessage(CreateNewPoly1Activity.this,
									getResources().getString(R.string.l_xb59));
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				break;
			case 3:
				Utils.ExitPrgress(CreateNewPoly1Activity.this);
				try {
					Utils.showMessage(CreateNewPoly1Activity.this,
							getResources().getString(R.string.l_xb59));
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.create_new_poly1);
		uid = Utils.getUid(CreateNewPoly1Activity.this);
		init();

		this.findViewById(R.id.return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});

		create_new_poly1_finish.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (panduan()) {
					createPhoto();
				} else {
					Utils.showMessage(CreateNewPoly1Activity.this,
							getResources().getString(R.string.l_xb61));
				}
			}
		});
	}

	/**
	 * 获取权限 0 仅自己 1.所有人 2.朋友
	 */
	public String getQuanXian(int jurisdiction) {
		// jurisdiction = 0;// 用于判断选择的什么权限（0所有人/1朋友/2仅自己）
		if (jurisdiction == 0) {
			return "1";
		} else if (jurisdiction == 1) {
			return "2";
		} else if (jurisdiction == 2) {
			return "0";
		}
		return "1";
	}

	/**
	 * 获取相册类型
	 */
	public String getPicType(int jurisdiction2) {// 相册类型：0：足迹，1：梦想，2：收藏(
		// private int jurisdiction2 = 10;//
		// 用于判断选择的什么版型（10足迹/11梦想/12收藏）
		if (jurisdiction2 == 10) {
			return "0";
		} else if (jurisdiction2 == 11) {
			return "1";
		} else if (jurisdiction2 == 12) {
			return "2";
		}
		return "2";
	}

	protected boolean panduan() {
		// TODO Auto-generated method stub
		if (!StringUtil.isNullOrEmpty(create_new_poly1_name.getText()
				.toString().trim())) {
			return true;
		} else
			return false;
	}

	/**
	 * 创建聚乐板
	 */
	protected void createPhoto() {
		// TODO Auto-generated method stub
		Utils.showPrgress(CreateNewPoly1Activity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				// Map<String, Object> map = new HashMap<String,
				// Object>();
				// String invite_content = Utils.sendRequest(map,
				// Constants.BadiUrl + "user_photo");
				String na = create_new_poly1_name.getText().toString().trim();
				String quanxian = getQuanXian(jurisdiction);
				String PicType = getPicType(jurisdiction2);
				String content = Utils.getRequest(Constants.BadiUrl_1
						+ "photo/createPhoto?uid=" + uid + "&name=" + na
						+ "&status=" + quanxian + "&img_type=" + PicType);

//				   ("用户id是:" + uid + Constants.BadiUrl_1
//						+ "photo/createPhoto?uid=" + uid + "&name=" + na
//						+ "&status=" + quanxian + "&img_type=" + PicType);
//				   
//						+ "photo/createPhoto?uid=" + uid + "&name=" + na
//						+ "&status=" + quanxian + "&img_type=" + PicType);
				// String content = Utils.getRequest(Constants.BadiUrl
				// + "article_img?rid=" + "121"+"&pageNum"+"1");
				// String content = Utils.sendRequest(map,
				// Constants.BadiUrl + "all_record_type");
				//    
				if (content != null) {
					results = Utils.checkResult_NNN(
							CreateNewPoly1Activity.this, content);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
//						   ("解析出的数据是：" + content);
						try {
							// list =
							// JSONUtils.getListByJsonString(results.getRetmsg(),
							// Pre_message_total.class);

							mHandler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {

						mHandler.sendEmptyMessage(3);
					}
				} else {
					mHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	public void init() {
		create_new_poly1_name = (EditText) this
				.findViewById(R.id.create_new_poly1_name);
		create_new_poly1_finish = (Button) findViewById(R.id.create_new_poly1_finish);
		create_new_poly1_tital = (TextView) findViewById(R.id.create_new_poly1_tital);
		// 下面的是相册权限的
		i_0 = (ImageView) findViewById(R.id.cnewpoly_quanxian_01);
		i_0.setVisibility(View.VISIBLE);
		i_1 = (ImageView) findViewById(R.id.cnewpoly_quanxian_02);
		i_1.setVisibility(View.GONE);
		i_2 = (ImageView) findViewById(R.id.cnewpoly_quanxian_03);
		i_2.setVisibility(View.GONE);

		ll_0 = (LinearLayout) findViewById(R.id.create_new_poly_layout01);
		ll_1 = (LinearLayout) findViewById(R.id.create_new_poly_layout02);
		ll_2 = (LinearLayout) findViewById(R.id.create_new_poly_layout03);

		ll_0.setOnClickListener(CreateNewPoly1Activity.this);
		ll_1.setOnClickListener(CreateNewPoly1Activity.this);
		ll_2.setOnClickListener(CreateNewPoly1Activity.this);
		setJurisdictionOnClickListener(jurisdiction);

		// 下面的全是版型的
		i_10 = (ImageView) findViewById(R.id.cnewpoly_quanxian_11);
		i_10.setVisibility(View.VISIBLE);
		i_11 = (ImageView) findViewById(R.id.cnewpoly_quanxian_12);
		i_11.setVisibility(View.GONE);
		i_12 = (ImageView) findViewById(R.id.cnewpoly_quanxian_13);
		i_12.setVisibility(View.GONE);

		ll_10 = (LinearLayout) findViewById(R.id.create_new_poly_layout11);
		ll_11 = (LinearLayout) findViewById(R.id.create_new_poly_layout12);
		ll_12 = (LinearLayout) findViewById(R.id.create_new_poly_layout13);

		ll_10.setOnClickListener(CreateNewPoly1Activity.this);
		ll_11.setOnClickListener(CreateNewPoly1Activity.this);
		ll_12.setOnClickListener(CreateNewPoly1Activity.this);

		setJurisdictionOnClickListener(jurisdiction2);

	}

	/**
	 * 用于更新选项
	 * 
	 * @param Click
	 *            (点击的是哪个layout)
	 */
	public void setJurisdictionOnClickListener(int Click) {
		switch (Click) {
		case 0:
			if (jurisdiction != 0)
				jurisdiction = 0;
			i_0.setVisibility(View.VISIBLE);
			i_1.setVisibility(View.GONE);
			i_2.setVisibility(View.GONE);
			break;
		case 1:
			if (jurisdiction != 1)
				jurisdiction = 1;
			i_0.setVisibility(View.GONE);
			i_1.setVisibility(View.VISIBLE);
			i_2.setVisibility(View.GONE);
			break;
		case 2:
			if (jurisdiction != 2)
				jurisdiction = 2;
			i_0.setVisibility(View.GONE);
			i_1.setVisibility(View.GONE);
			i_2.setVisibility(View.VISIBLE);
			break;

		case 10:
			if (jurisdiction2 != 10)
				jurisdiction2 = 10;
			i_10.setVisibility(View.VISIBLE);
			i_11.setVisibility(View.GONE);
			i_12.setVisibility(View.GONE);
			break;
		case 11:
			if (jurisdiction2 != 11)
				jurisdiction2 = 11;
			i_10.setVisibility(View.GONE);
			i_11.setVisibility(View.VISIBLE);
			i_12.setVisibility(View.GONE);
			break;
		case 12:
			if (jurisdiction2 != 12)
				jurisdiction2 = 12;
			i_10.setVisibility(View.GONE);
			i_11.setVisibility(View.GONE);
			i_12.setVisibility(View.VISIBLE);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.create_new_poly_layout01:
			setJurisdictionOnClickListener(0);
			break;
		case R.id.create_new_poly_layout02:
			setJurisdictionOnClickListener(1);
			break;
		case R.id.create_new_poly_layout03:
			setJurisdictionOnClickListener(2);
			break;

		case R.id.create_new_poly_layout11:
			setJurisdictionOnClickListener(10);
			break;
		case R.id.create_new_poly_layout12:
			setJurisdictionOnClickListener(11);
			break;
		case R.id.create_new_poly_layout13:
			setJurisdictionOnClickListener(12);
			break;

		}
	}

}
